import 'package:flutter/material.dart';

class FormInput extends StatefulWidget {
  const FormInput(
      {super.key,
      this.height,
      this.width,
      this.keyboardType,
      this.controller,
      this.focusNode,
      this.maxLength,
      this.hintText,
      this.onChanged,
      this.maxLines,
      this.labelText,
      this.suffixText,
      this.enabled,
      this.initialValue,
      this.prefixText,
      this.onEditingComplete,
      this.suffix,
      this.validator,
      this.fillded = true,
      this.textInputAction = TextInputAction.next,
      this.onFieldSubmitted,
      this.autovalidateMode = AutovalidateMode.onUserInteraction});
  final double? height;
  final double? width;
  final TextInputType? keyboardType;
  final TextEditingController? controller;
  final FocusNode? focusNode;
  final int? maxLength;
  final String? hintText;
  final ValueChanged<String>? onChanged;
  final int? maxLines;
  final String? labelText;
  final String? suffixText;
  final bool? enabled;
  final String? initialValue;
  final String? prefixText;
  final VoidCallback? onEditingComplete;
  final Widget? suffix;
  final TextInputAction? textInputAction;
  final ValueChanged<String>? onFieldSubmitted;
  final FormFieldValidator<String>? validator;
  final AutovalidateMode autovalidateMode;
  final bool fillded;

  @override
  State<FormInput> createState() => _FormInputState();
}

class _FormInputState extends State<FormInput> {
  late TextEditingController? _controller;
  bool _isControllerCreatedBySelf = false;
  FocusNode? _focusNode;
  bool _hasText = false;
  @override
  void initState() {
    super.initState();
    if (widget.controller != null) {
      _controller = widget.controller!;

      _isControllerCreatedBySelf = false;
    } else {
      _controller = TextEditingController(text: widget.initialValue);
      _isControllerCreatedBySelf = true;
    }
    _hasText = _controller!.value.text.isNotEmpty;
    _focusNode = widget.focusNode;
  }

  @override
  void didUpdateWidget(covariant FormInput oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (_isControllerCreatedBySelf) {
      if (oldWidget.initialValue != widget.initialValue) {
        _controller!.dispose();
        _controller = null;
        _controller = TextEditingController(text: widget.initialValue);
      }
    } else {
      if (oldWidget.controller != widget.controller) {
        _controller = widget.controller;
      }
    }
    if (widget.focusNode != oldWidget.focusNode) {
      _focusNode;
    }
  }

  @override
  void dispose() {
    if (_isControllerCreatedBySelf && _controller != null) {
      _controller!.dispose();
      _controller = null;
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: widget.height,
      width: widget.width,
      child: TextFormField(
          maxLines: widget.maxLines,
          keyboardType: widget.keyboardType,
          controller: _controller,
          focusNode: _focusNode,
          maxLength: widget.maxLength,
          enabled: widget.enabled,
          validator: widget.validator,
          onEditingComplete: widget.onEditingComplete,
          textInputAction: widget.textInputAction,
          onFieldSubmitted: widget.onFieldSubmitted,
          autovalidateMode: widget.autovalidateMode,
          decoration: InputDecoration(
              hintText: widget.hintText,
              hintStyle: Theme.of(context).textTheme.bodyLarge?.copyWith(
                  color: Theme.of(context)
                      .textTheme
                      .bodyLarge
                      ?.color
                      ?.withAlpha(150)),
              suffixText: widget.suffixText,
              prefixText: widget.prefixText,
              suffix: widget.suffix,
              errorStyle: Theme.of(context)
                  .textTheme
                  .bodyLarge!
                  .copyWith(color: Colors.red),
              contentPadding: const EdgeInsets.all(10),
              suffixIconConstraints: const BoxConstraints(
                  minWidth: 24, minHeight: 24, maxHeight: 32, maxWidth: 48),
              suffixIcon: _hasText
                  ? InkWell(
                      onTap: () {
                        _controller!.text = "";
                        setState(() {
                          _hasText = false;
                        });
                      },
                      child: const Icon(
                        Icons.close,
                        size: 15,
                      ),
                    )
                  : null,
              // suffixIconConstraints:
              //     BoxConstraints(maxWidth: 30, maxHeight: 30),
              isDense: true,
              filled: widget.fillded,
              labelText: widget.labelText,
              border: InputBorder.none),
          onChanged: (value) {
            setState(() {
              _hasText = value.isNotEmpty;
            });
            if (widget.onChanged != null) {
              widget.onChanged!(value);
            }
          }),
    );
  }
}
